﻿CREATE PROCEDURE [dbo].[proc_Assets_Create]
	(
		@CategoryId int,
		@NameId int,
		@Description nvarchar(500),
		@CompanyId int,
		@DepartmentId int,
		@UserName nvarchar(50),
		@EName nvarchar(50),
		@StateId int,
		@Prices numeric(18,2),
		@DJUserName nvarchar(50),
		@DJDate datetime,
		@GGXH nvarchar(50),
		@GZDate datetime,
		@Number int,
		@zcbh VARCHAR(50),
		@memo NVARCHAR(2000),
		@userid VARCHAR(30),
		@username_1 NVARCHAR(50)
	)
AS
begin
	--资产编号
	declare @Anumber varchar(10),@tempNumber varchar(10)				
	declare @i int
	Set @i=0
	While @i<@Number
		begin
			--生成资产编号
			if((select count(id) from Assets)=0)
			set @Anumber = 'LC' + '00000001'
			else
			begin
				set @tempNumber = (select top 1 Anumber from Assets order by convert(integer,substring(Anumber,3,8)) desc)
				set @Anumber = convert(integer,substring(@tempNumber,3,8)) + 1
				if (len(@Anumber)=1)
					set @Anumber = 'LC' +'0000000' + @Anumber
				else if (len(@Anumber)=2)
					set @Anumber = 'LC' +'000000' + @Anumber
				else if (len(@Anumber)=3)
					set @Anumber = 'LC' +'00000' + @Anumber
				else if (len(@Anumber)=4)
					set @Anumber = 'LC' +'0000' + @Anumber
				else if (len(@Anumber)=5)
					set @Anumber = 'LC' +'000' + @Anumber
				else if (len(@Anumber)=6)
					set @Anumber = 'LC' +'00' + @Anumber
				else if (len(@Anumber)=7)
					set @Anumber = 'LC' +'0' + @Anumber
				else
					set @Anumber = 'LC' + @Anumber
			end
		
			Insert Into Assets
			(
				CategoryId,
				NameId,
				Description,
				CompanyId,
				DepartmentId,
				UserName,
				EName,
				StateId,
				Prices,
				DJUserName,
				DJDate,
				GGXH,
				GZDate,
				ANumber
			)
			Values
			(
				@CategoryId,
				@NameId,
				@Description,
				@CompanyId,
				@DepartmentId,
				@UserName,
				@EName,
				@StateId,
				@Prices,
				@DJUserName,
				@DJDate,
				@GGXH,
				@GZDate,
				@Anumber
			)
			Set @i=@i+1
			
			INSERT INTO [zcjl]
           ([zcbh]
           ,[memo]
           ,[lrrq]
           ,[userid]
           ,[username])
     VALUES
           (@Anumber
           ,@memo
           ,GETDATE()
           ,@userid
           ,@username_1)			
			
		END
		
end
	RETURN
